package com.hhx.baselibrary.utils;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;


/**
 * Created by MQ on 2017/5/3.
 */

public class CommonUtil {
    /**
     * 测量View的宽高
     *
     * @param view View
     */
    public static void measureWidthAndHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
    }


    // 动态改变ImageView bitmap颜色
    // 实现方法:
    public static Bitmap changeBitmapColor(Bitmap sourceBitmap, String aimColorStr) {
//                    验证参数合理
        if (sourceBitmap == null || sourceBitmap.isRecycled()) {
            throw new RuntimeException("source exception!!!");
        }
        int aimColor;
        try {
            aimColor = Color.parseColor(aimColorStr.trim());
        } catch (Exception e) {
            throw new RuntimeException("aimColorStr error!!!");
        }
        //按照图标的大小创建数组
        int mBitmapWidth = sourceBitmap.getWidth();
        int mBitmapHeight = sourceBitmap.getHeight();
        int mArrayColorLengh = mBitmapWidth * mBitmapHeight;
        int[] mArrayColor = new int[mArrayColorLengh];
        //循环bitmap 的每个像素点,查看alpha值
        int count = 0;
        for (int i = 0; i < mBitmapHeight; i++) {
            for (int j = 0; j < mBitmapWidth; j++) {
                //获得Bitmap 图片中每一个点的color颜色值
                int color = sourceBitmap.getPixel(j, i);
                int a = Color.alpha(color);
                if (a != 0) {
                    //不等于0 即不透明部分,设置成我们想要的颜色
                    mArrayColor[count] = aimColor;
                } else {
                    //透明仍然为透明
                    int aimColor2 = Color.parseColor("#00000000");
                    mArrayColor[count] = aimColor2;
                }
                count++;
            }
        }
        //根据数组创建新的Bitmap
        Bitmap newBitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
        newBitmap.setPixels(mArrayColor, 0, mBitmapWidth, 0, 0, mBitmapWidth, mBitmapHeight);
        return newBitmap;
    }

}
